package io.github.maojian.temp;

import java.util.HashSet;

/**
 * @Auther: jian.mao
 * @Date: 2022-07-06 10:28
 * @Description:
 */
public class FruitCollection {
    /**
     * 计算给定的年份和月份，两位小伙伴当月采摘的水果总量，本方法按照竞赛题目要求完成
     * @param mingRecords 小明当年的上山记录
     * @param tianRecords 小天当年的上山记录
     * @param year 给定的年份
     * @param month 给定的月份
     * @return 给定的年份和月份，两位小伙伴当月采摘的水果总量
     */
    public long computeYield(int[] mingRecords, int[] tianRecords, int year, int month) {
        int[] curStartAndNnd = getStartAndEndDate(year, month);
        int start = curStartAndNnd[0];
        int end = curStartAndNnd[1];
        HashSet<Integer> hashSet = new HashSet();
        for (int i = 0; i < mingRecords.length; i++) {
            hashSet.add(mingRecords[i]);
        }
        for (int i = 0; i < tianRecords.length; i++) {
            hashSet.add(tianRecords[i]);
        }
        int count = 0;
        for (Integer record : hashSet) {
            if (record >= start && record <= end) {
                count += Math.pow(2, record - start);
            }
        }
        return count;
    }

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        int[] mingRecords = new int[]{1, 3, 33, 45, 100};
        int[] tianRecords = new int[]{1, 4, 5, 99, 200};
        int year = 2020;
        int month = 1;
        FruitCollection solution = new FruitCollection();
        System.out.println(solution.computeYield(mingRecords, tianRecords, year, month));

    }

    public int[] getStartAndEndDate(int year, int month) {
        int[] leapYear = new int[]{0, 31, 60, 91, 121, 152, 182, 213, 244, 274, 305, 335, 366};
        int[] nonleapYear = new int[]{0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365};
        if (year % 4 == 0) {
            return new int[]{leapYear[month - 1], leapYear[month]};
        } else {
            return new int[]{nonleapYear[month - 1], nonleapYear[month]};
        }
    }
}
